package com.yue.service.impl;

import com.yue.domain.User;
import com.yue.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MongoTemplate mongoTemplate;
    public User findByName(String name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("name").is(name));
        return mongoTemplate.findOne(query,User.class);
    }

    @Override
    public User addUser() {
        User user = new User();
        user.setName("liangliang");
        user.setAge(33);
        return mongoTemplate.save(user);
    }

    @Override
    public void deleteUser(String name) {
        mongoTemplate.remove(new Query().addCriteria(Criteria.where("name").is(name)),User.class);
    }

    @Override
    public User addUser(User user) {
        return mongoTemplate.save(user);
    }
}
